﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SuperMap.Data.Conversion;
using SuperMap.Desktop;
using System.IO;
using SuperMap.Data;
using SuperMap.Mapping;

namespace SuperMap.HB.ThemeWork
{
    public partial class MakeMapControl : UserControl
    {
        private RegionInfo m_RegionInfo;

        private const string m_DataSourceName = "基础数据";
        private Datasource m_DataSource = Desktop.Application.ActiveApplication.Workspace.Datasources[m_DataSourceName];

        public MakeMapControl()
        {
            InitializeComponent();
            m_RegionInfo = new RegionInfo
            {
                RegionType = RegionType.Unique
            };

            groupBox1.Paint += groupBox_Paint;
            groupBox2.Paint += groupBox_Paint;
        }

        private void groupBox_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                GroupBox gBox = (GroupBox)sender;

                e.Graphics.Clear(gBox.BackColor);
                e.Graphics.DrawString(gBox.Text, gBox.Font, Brushes.Black, 10, 1);
                var vSize = e.Graphics.MeasureString(gBox.Text, gBox.Font);
                e.Graphics.DrawLine(Pens.Black, 1, vSize.Height / 2, 8, vSize.Height / 2);
                e.Graphics.DrawLine(Pens.Black, vSize.Width + 8, vSize.Height / 2, gBox.Width - 2, vSize.Height / 2);
                e.Graphics.DrawLine(Pens.Black, 1, vSize.Height / 2, 1, gBox.Height - 2);
                e.Graphics.DrawLine(Pens.Black, 1, gBox.Height - 2, gBox.Width - 2, gBox.Height - 2);
                e.Graphics.DrawLine(Pens.Black, gBox.Width - 2, vSize.Height / 2, gBox.Width - 2, gBox.Height - 2);
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void RegionThemeUniqueControl_Load(object sender, EventArgs e)
        {
            try
            {
                cmb_SelectDataSet.Items.Clear();
                foreach (Dataset dt in m_DataSource.Datasets)
                {
                    if (dt.Type == DatasetType.Region)
                    {
                        cmb_SelectDataSet.Items.Add(dt.Name);
                    }
                }
                if (cmb_SelectDataSet.Items.Count > 0)
                {
                    cmb_SelectDataSet.SelectedIndex = 0;
                    cmb_SelectField.Items.Clear();
                    DatasetVector dtv = m_DataSource.Datasets[cmb_SelectDataSet.Text] as DatasetVector;
                    foreach (FieldInfo info in dtv.FieldInfos)
                    {
                        if (!info.Name.ToLower().Contains("sm"))
                        {
                            cmb_SelectField.Items.Add(info.Name);
                        }
                    }
                    if (cmb_SelectField.Items.Count > 0)
                    {
                        cmb_SelectField.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btn_SelectPath_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txt_FilePath.Text = openFileDialog.FileName;
                    string datasetName = m_DataSource.Datasets.GetAvailableDatasetName((new FileInfo(txt_FilePath.Text)).Name.Split('.')[0]);
                    m_RegionInfo.DatasetName = datasetName;
                    ImportDataManager import = new ImportDataManager();
                    import.OnImportFinished += Import_OnImportFinished;
                    import.Import(m_DataSourceName, datasetName, openFileDialog.FileName);
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void Import_OnImportFinished(object sender, EventArgs e)
        {
            try
            {
                cmb_SelectField.Items.Clear();
                DatasetVector dtv = m_DataSource.Datasets[m_RegionInfo.DatasetName] as DatasetVector;
                foreach (FieldInfo info in dtv.FieldInfos)
                {
                    if (!info.IsSystemField)
                    {
                        cmb_SelectField.Items.Add(info.Name);
                    }
                }
                if (cmb_SelectField.Items.Count > 0)
                {
                    cmb_SelectField.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void rdo_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rdo_New.Checked)
                {
                    txt_FilePath.Visible = true;
                    cmb_SelectDataSet.Visible = false;
                    btn_SelectPath.Visible = true;
                }
                if (rdo_Import.Checked)
                {
                    txt_FilePath.Visible = false;
                    cmb_SelectDataSet.Visible = true;
                    btn_SelectPath.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_SelectDataSet_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_RegionInfo.DatasetName = cmb_SelectDataSet.Text;
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_SelectField_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_RegionInfo.ThemeFieldName = cmb_SelectField.Text;
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txt_MapName.Text))
                {
                    Desktop.Application.ActiveApplication.MessageBox.Show("请输入地图名称！");
                    return;
                }
                if (string.IsNullOrEmpty(m_RegionInfo.DatasetName))
                {
                    Desktop.Application.ActiveApplication.MessageBox.Show("请设置矢量数据！");
                    return;
                }
                if (string.IsNullOrEmpty(m_RegionInfo.ThemeFieldName))
                {
                    Desktop.Application.ActiveApplication.MessageBox.Show("请选择字段！");
                    return;
                }
                ThemeUnique theme = new ThemeUnique
                {
                    UniqueExpression = m_RegionInfo.ThemeFieldName
                };
                string xml = "";
                StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 4) + @"Templates\ThemeWork\Theme\行政区划面_名称单值专题图.xml");
                string str;
                while ((str = sr.ReadLine()) != null)
                {
                    xml += str;
                }
                theme.FromXML(xml);
                
                Desktop.Application.ActiveApplication.MainForm.FormManager.CloseAll(false);

                IFormMap formMap = Desktop.Application.ActiveApplication.CreateMapWindow();
                Desktop.Application.ActiveApplication.MainForm.MdiTabGroupManager.FindMdiTab(formMap.MapControl.ParentForm).Text = txt_MapName.Text.Trim();
                formMap.MapControl.Map.Name = txt_MapName.Text.Trim();
                formMap.MapControl.Map.Layers.Add(m_DataSource.Datasets[m_RegionInfo.DatasetName], theme, true);

                if (chk_ShowCityName.Checked)
                {
                    formMap.MapControl.Map.Layers.Add(m_DataSource.Datasets[m_RegionInfo.CityName], true);
                }
                if (chk_ShowRiverName.Checked)
                {
                    formMap.MapControl.Map.Layers.Add(m_DataSource.Datasets[m_RegionInfo.RiverName], true);
                }

                formMap.MapControl.Map.ViewEntire();
                bool exist = false;
                foreach (string map in Desktop.Application.ActiveApplication.Workspace.Maps)
                {
                    if (map.Equals(txt_MapName.Text.Trim()))
                    {
                        exist = true;
                        break;
                    }
                }
                if (exist)
                {
                    Desktop.Application.ActiveApplication.Workspace.Maps.SetMapXML(txt_MapName.Text.Trim(), formMap.MapControl.Map.ToXML());
                }
                else
                {
                    Desktop.Application.ActiveApplication.Workspace.Maps.Add(txt_MapName.Text.Trim(), formMap.MapControl.Map.ToXML());
                }
                if (Desktop.Application.ActiveApplication.Workspace.Save())
                {
                    Desktop.Application.ActiveApplication.MessageBox.Show("保存成功！");
                }
                else
                {
                    Desktop.Application.ActiveApplication.MessageBox.Show("保存失败！");
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btn_SetStyle_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }
    }
}
